package com.mdd.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.entity.system.LsConfig;
import com.mdd.common.mapper.system.LsConfigMapper;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库配置操作工具
 */
public class LsConfigUtil {

    /**
     * 根据类型获取配置
     *
     * @author panwieliang
     * @param type 类型
     * @return Map<String, String>
     */
    public static Map<String, String> get(String type) {
        LsConfigMapper model = SpringUtil.getBean(LsConfigMapper.class);

        List<LsConfig> configs = model.selectList(
                new QueryWrapper<LsConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type));

        Map<String, String> map = new LinkedHashMap<>();
        for (LsConfig config : configs) {
            map.put(config.getName(), config.getValue());
        }

        return map;
    }

    /**
     * 根据类型和名称获取配置
     *
     * @author panweiliang
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static String get(String type, String name) {
        LsConfigMapper model = SpringUtil.getBean(LsConfigMapper.class);

        LsConfig config = model.selectOne(
                new QueryWrapper<LsConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type)
                        .eq("name", name));

        return config.getValue();
    }

    /**
     * 根据类型和名称获取配置
     *
     * @author panweiliang
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static String get(String type, String name, String defaults) {
        LsConfigMapper model = SpringUtil.getBean(LsConfigMapper.class);

        LsConfig config = model.selectOne(
                new QueryWrapper<LsConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type)
                        .eq("name", name));

        if (config == null) {
            return defaults;
        }

        String value = config.getValue();
        if (StringUtil.isNotEmpty(value) && !"null".equals(value)) {
            return config.getValue();
        }

        return defaults;
    }

    /**
     * 根据类型和名称获取配置(JSON自定转Map)
     *
     * @author panweiliang
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static Map<String, String> getMap(String type, String name) {
        LsConfigMapper model = SpringUtil.getBean(LsConfigMapper.class);

        LsConfig config = model.selectOne(
                new QueryWrapper<LsConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type)
                        .eq("name", name));

        if (config == null) {
            return null;
        }

        if (config.getValue().equals("") || config.getValue().equals("[]") || config.getValue().equals("{}")) {
            return new LinkedHashMap<>();
        }

        return ToolsUtil.jsonToMap(config.getValue());
    }

    /**
     * 设置配置的值
     *
     * @author panweiliang
     * @param type 类型
     * @param name 名称
     * @param val 值
     */
    public static void set(String type, String name, String val) {
        LsConfigMapper model = SpringUtil.getBean(LsConfigMapper.class);

        LsConfig config = model.selectOne(
                new QueryWrapper<LsConfig>()
                        .eq("type", type)
                        .eq("name", name));

        if (config != null) {
            config.setValue(val);
            config.setUpdateTime(System.currentTimeMillis() / 1000);
            model.updateById(config);
        } else {
            LsConfig lsConfig = new LsConfig();
            lsConfig.setType(type);
            lsConfig.setName(name);
            lsConfig.setValue(val);
            lsConfig.setCreateTime(System.currentTimeMillis() / 1000);
            lsConfig.setUpdateTime(System.currentTimeMillis() / 1000);
            model.insert(lsConfig);
        }
    }

}
